Borland Online And The Cobb Group Present:


March, 1995 - Vol. 2 No. 3

Validator review

Borland ships several validator classes with Borland C++ that you can use to verify user input in an edit control. Each class derives from a common base class, TValidator. Figure A illustrates the inheritance relationships of the validator classes.

Figure A - Each validator class derives from the TValidator base class and inherits some of its default behavior.

The TValidator class declares four member functions that define the primary interface between the validator classes and the TEdit interface class:

Valid()
IsValid()
IsValidInput()
Error()

Here, we'll review the behavior of each member function in the TValidator base class and the behavior of each class's derived versions of these functions.

All of these functions are virtual, except for the Valid() member function. Because this function isn't virtual, you won't be able to override its behavior in derived classes­­you'll only be able to hide it. (This means the corresponding TEdit object will always call the TValidator version of the Valid() function.)

TValidator

TValidator is the base class for all validator classes.

TFilterValidator

The TFilterValidator class derives directly from the TValidator class.

  • IsValid()­­This version checks characters as the user enters them. If a character belongs to the valid character set, it returns True. Otherwise, it returns False.
  • IsValidInput()­­This version checks characters as the user enters them. If a character belongs to the valid character set, it returns True. Otherwise, it returns False.
  • Error()­­This version loads the IDS_VALINVALIDCHAR string resource and displays this string in a message box.

TRangeValidator

The TRangeValidator class derives directly from the TFilterValidator class (to prevent nonnumeric input).

  • IsValid()­­This function returns True if the numeric value in the edit control is within the range of the validator.
  • Error()­­This version loads the IDS_VALNOTINRANGE string resource and displays this string in a message box along with the maximum and minimum values.

TLookupValidator

The TLookupValidator class derives directly from the TValidator class and will serve as a base class for any validator class that needs to search a table, list, or database for valid input.

  • IsValid()­­This function returns the result of a new Lookup() member function. By default, the Lookup() member function simply returns True.

TStringLookupValidator

The TStringLookupValidator class derives directly from the TLookupValidator class and overrides the TLookupValidator::Lookup() member function to search a sorted array of strings.

  • Error()­­This version loads the IDS_VALNOTINLIST string resource and displays this string in a message box.

TPXPictureValidator

The TPXPictureValidator class derives directly from the TValidator class.

  • IsValid()­­This function returns True if the edit control is empty or if the edit text matches the pattern of the picture string.
  • IsValidInput()­­This version returns True if the edit control is empty or if the text is valid for each position so far. If the suppressFill parameter (the second parameter) is False and the autoFill option of the validator is set to True, this function will automatically fill in literal characters. Otherwise, it will add only the character the user enters.
  • Error()­­This version loads the IDS_VALPXPCONFORM string resource and displays this string and the current picture string in a message box.

Return to the Borland C++ Developer's Journal index

Subscribe to the Borland C++ Developer's Journal


Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.